package de.szut.dqi12.sqlitebrowser.model;

import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import de.szut.dqi12.sqlitebrowser.controller.ErrorMessages;
import de.szut.dqi12.sqlitebrowser.controller.GUIController;

/**
 * PropertiesAnalyzer
 * Reads and writes the propertie file
 * @author Alexander
 */
public class PropertiesAnalyzer {

	private Properties propertie;
	private File propertieFile;
	private Rectangle frameBounds;
	private String lastDatabaseConnectionPath;
	private FileInputStream reader;
	
	/**
	 * Path to the propertie file
	 */
	
	public static final String FILE_PATH = "cfg/properties.ini";
	
	/**
	 * Propertie list keys
	 */
	
	public static final String POSITION_X_KEY = "frame.position.x";
	public static final String POSITION_Y_KEY = "frame.position.y";
	public static final String SIZE_HEIGHT_KEY = "frame.height";
	public static final String SIZE_WIDTH_KEY = "frame.width";
	public static final String LAST_DATABASE_CONNECTION_KEY = "last.database.connection";
	
	
	/**
	 * Constructor of the PropertiesAnalyzer class
	 * Initialize the reader, writer and the propertie file
	 * Reads all values out of this property file
	 */
	public PropertiesAnalyzer() {
		propertie = new Properties();
		propertieFile = new File(FILE_PATH);
		frameBounds = new Rectangle();
		try {
			reader = new FileInputStream(propertieFile);
			propertie.load(reader);
			frameBounds.setBounds(
					Double.valueOf(propertie.getProperty(POSITION_X_KEY)).intValue(), 
					Double.valueOf(propertie.getProperty(POSITION_Y_KEY)).intValue(),
					Double.valueOf(propertie.getProperty(SIZE_WIDTH_KEY)).intValue(), 
					Double.valueOf(propertie.getProperty(SIZE_HEIGHT_KEY)).intValue());
		} catch (Exception e) {
			frameBounds.setBounds(100, 100, 868, 582);
		}
		try {
			lastDatabaseConnectionPath = propertie.getProperty(LAST_DATABASE_CONNECTION_KEY);
			if(lastDatabaseConnectionPath == null) {
				lastDatabaseConnectionPath = System.getProperty("user.home");
			}
		} catch(Exception e) {
			lastDatabaseConnectionPath = System.getProperty("user.home");
		}
	}
 	
	/**
	 * Gets the frame size and position
	 * @return A rectangle with position and size data
	 */
	
	public Rectangle getFrameBounds() {
		return frameBounds;
	}
	
	/**
	 * Gets the path of the last displayed database
	 * @return The path as a String
	 */
	
	public String getLastDatabaseConnectionPath() {
		return lastDatabaseConnectionPath;
	}
	
	/**
	 * Updates or sets the Frame size and position in the property file
	 * @param actualBounds - A rectangle with position and size data
	 */
	
	public void setFrameBounds(Rectangle actualBounds) {
		frameBounds = actualBounds;
		propertie.setProperty(POSITION_X_KEY, String.valueOf(frameBounds.getX()));
		propertie.setProperty(POSITION_Y_KEY, String.valueOf(frameBounds.getY()));	
		propertie.setProperty(SIZE_WIDTH_KEY, String.valueOf(frameBounds.getWidth()));
		propertie.setProperty(SIZE_HEIGHT_KEY, String.valueOf(frameBounds.getHeight()));
		store();
	}
	
	/**
	 * Updates or sets the last displayed Database
	 * @param path - The path as a String
	 */
	
	public void setLastDatabaseConnection(String path) {
		lastDatabaseConnectionPath = path;
		propertie.setProperty(LAST_DATABASE_CONNECTION_KEY, lastDatabaseConnectionPath);		
		store();
	}
	
	/**
	 * Writes the data into the Propertie file
	 */
	
	public void store() {
		try {
			propertie.store(new FileOutputStream(propertieFile), null);
		} catch (IOException e) {
			GUIController.getInstance().executeErrorMessageDialog(ErrorMessages.WRITING_PROPERTY_LIST_FAILED);
		}
	}
	
	
}
